package com.xbongbong.paas.controller;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.help.menu.InitWorkOrderHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateCategoryDelDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateCategorySaveDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateCategorySortDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateDelDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateDetailDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateEnableDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateListDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateSaveDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateCategoryDelVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateCategorySaveVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateCategorySortVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateDelVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateDetailVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateEnableVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateListVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateSaveVO;
import com.xbongbong.saas.service.WorkOrderTemplateService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Objects;

/**
 * 工单设置相关cotroller
 * @author 魏荣杰
 * @date 2019/3/5 10:53
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workorder/set")
public class WorkOrderSettingController {
    @Resource
    private WorkOrderTemplateService workOrderTemplateService;
    @Resource
    private InitWorkOrderHelp initWorkOrderHelp;
    /**
     * Description: 获取工单分类以及其模板
     * @param workOrderTemplateListDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/5 10:59
     * @since v1.0
     */
    @RequestMapping(value = "/templateList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateList(@RequestBody @Valid WorkOrderTemplateListDTO workOrderTemplateListDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateListVO workOrderTemplateListVO = workOrderTemplateService.getListTree(workOrderTemplateListDTO.getCorpid(), workOrderTemplateListDTO.getAppId());
                response = new XbbResponse<>(workOrderTemplateListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 保存工单分类
     * @param workOrderTemplateCategorySaveDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/7 15:32
     * @since v1.0
     */
    @RequestMapping(value = "/saveCategory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveCategory(@RequestBody @Valid WorkOrderTemplateCategorySaveDTO workOrderTemplateCategorySaveDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateCategorySaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateCategorySaveVO workOrderTemplateCategorySaveVO = workOrderTemplateService.saveCategory(workOrderTemplateCategorySaveDTO);
                response = new XbbResponse<>(workOrderTemplateCategorySaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 删除工单模板分类
     * @param workOrderTemplateCategoryDelDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/18 10:22
     * @since v1.0
     */
    @RequestMapping(value = "/delCategory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delCategory(@RequestBody @Valid WorkOrderTemplateCategoryDelDTO workOrderTemplateCategoryDelDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateCategoryDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateCategoryDelVO workOrderTemplateCategoryDelVO = workOrderTemplateService.delCategory(workOrderTemplateCategoryDelDTO);
                response = new XbbResponse<>(workOrderTemplateCategoryDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 保存工单分类排序
     * @param workOrderTemplateCategorySortDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/18 10:23
     * @since v1.0
     */
    @RequestMapping(value = "/sortCategory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sortCategory(@RequestBody @Valid WorkOrderTemplateCategorySortDTO workOrderTemplateCategorySortDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateCategorySortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateCategorySortVO workOrderTemplateCategorySortVO = workOrderTemplateService.sortCategory(workOrderTemplateCategorySortDTO);
                response = new XbbResponse<>(workOrderTemplateCategorySortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 获取工单模板详情
     * @param workOrderTemplateDetailDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/6 11:23
     * @since v1.0
     */
    @RequestMapping(value = "/templateDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateDetail(@RequestBody @Valid WorkOrderTemplateDetailDTO workOrderTemplateDetailDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateDetailVO workOrderTemplateDetailVO = workOrderTemplateService.getTemplateDetail(workOrderTemplateDetailDTO);
                response = new XbbResponse<>(workOrderTemplateDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * Description: 保存工单模板
     * @param workOrderTemplateSaveDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/12 15:29
     * @since v1.0
     */
    @RequestMapping(value = "/saveTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveTemplate(@RequestBody @Valid WorkOrderTemplateSaveDTO workOrderTemplateSaveDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateSaveVO workOrderTemplateSaveVO = workOrderTemplateService.saveTemplate(workOrderTemplateSaveDTO);
                response = new XbbResponse<>(workOrderTemplateSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * Description: 开关模板
     * @param workOrderTemplateEnableDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/18 17:31
     * @since v1.0
     */
    @RequestMapping(value = "/enableTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enableTemplate(@RequestBody @Valid WorkOrderTemplateEnableDTO workOrderTemplateEnableDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateEnableVO workOrderTemplateEnableVO = workOrderTemplateService.enableTemplate(workOrderTemplateEnableDTO);
                response = new XbbResponse<>(workOrderTemplateEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 删除模板
     * @param workOrderTemplateDelDTO
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/3/18 17:31
     * @since v1.0
     */
    @RequestMapping(value = "/delTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delTemplate(@RequestBody @Valid WorkOrderTemplateDelDTO workOrderTemplateDelDTO, BindingResult br) throws Exception {
        XbbResponse<WorkOrderTemplateDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateDelVO workOrderTemplateDelVO = workOrderTemplateService.delTemplate(workOrderTemplateDelDTO);
                response = new XbbResponse<>(workOrderTemplateDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String init(@RequestBody @Valid MenuInitDTO menuInitDTO, BindingResult br) throws Exception {
        XbbResponse<MenuInitVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuInitVO menuInitVO = initWorkOrderHelp.initMenu(menuInitDTO, new HashMap<>());
                response = new XbbResponse<>(menuInitVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
